<template>
  <el-dialog
    title="路径配置"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" label-width="140px" label-position="right">
      <el-form-item label="密云站GRAS3配置">
        <el-select v-model="form.MiyunvalueGRAS3" placeholder="请选择" style="width: 320px;">
          <el-option v-for="item in form.MiyunOptionsGRAS3" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密云站GRAS5配置">
        <el-select v-model="form.MiyunvalueGRAS5" placeholder="请选择" style="width: 320px;">
          <el-option v-for="item in form.MiyunOptionsGRAS5" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="武清站GRAS4配置">
        <el-select v-model="form.wuqingvalueGRAS4" placeholder="请选择" style="width: 320px;">
          <el-option v-for="item in form.wuqingOptionsGRAS4" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="submitLoading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import config from "@/utils/config";
const { DMSip } = config;

export default {
  name: 'PathConfigDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      submitLoading: false,
      // 目录配置表单
      form: {
        //密云目录GRAS3
        MiyunOptionsGRAS3: [],
        MiyunvalueGRAS3: '',
        //密云目录GRAS5
        MiyunOptionsGRAS5: [],
        MiyunvalueGRAS5: '',
        //武清目录GRAS4
        wuqingOptionsGRAS4: [],
        wuqingvalueGRAS4: '',
      }
    }
  },
  watch: {
    visible: {
      immediate: true,
      handler(val) {
        this.dialogVisible = val
        if (val) {
          this.getUrl()
        }
      }
    },
    dialogVisible(val) {
      this.$emit('update:visible', val)
    }
  },
  methods: {
    // 生效日期选择
    dataselectFn(e) {
      console.log(e);
    },
    // 获取配置信息
    getUrl() {
      this.$axios({
        method: "post",
        url: DMSip + '/-/bin.preData.get_all_conf',
      }).then(res => {
        //密云 GRAS3
        let myurlobjarrGRAS3 = []
        let MYurldataGRAS3 = res.data.data.my.filter((gras) => (gras.gras == 'GRAS3'))
        for (let i = 0; i < MYurldataGRAS3.length; i++) {
          //设备数组
          let mydevlistGRAS3 = MYurldataGRAS3[i].dev
          //设备默认
          let GRAS3devvalue = mydevlistGRAS3.filter((devitem) => (devitem.default == 1));
          //通道默认
          let GRAS3chvalue = mydevlistGRAS3.filter((GRAS3chvalue) => (GRAS3chvalue.name == GRAS3devvalue[0].name))[0].CH.filter((grs3chitem) => (grs3chitem.default == 1))
          // 默认选中项去掉GRAS3
          this.form.MiyunvalueGRAS3 = 'my/' + GRAS3devvalue[0].name + '/' + GRAS3chvalue[0].name

          for (let p = 0; p < mydevlistGRAS3.length; p++) {
            //通道数组
            let mychlistGRAS3 = mydevlistGRAS3[p].CH
            for (let k = 0; k < mychlistGRAS3.length; k++) {
              let myurlobjGRAS3 = {
                value: 'my/' + mydevlistGRAS3[p].name + '/' + mychlistGRAS3[k].name,
                label: mydevlistGRAS3[p].name + '/' + mychlistGRAS3[k].name,
              }
              myurlobjarrGRAS3.push(myurlobjGRAS3)
            }
          }
        }
        this.form.MiyunOptionsGRAS3 = myurlobjarrGRAS3

        //密云 GRAS5
        let myurlobjarrGRAS5 = []
        let MYurldataGRAS5 = res.data.data.my.filter((gras) => (gras.gras == 'GRAS5'))
        for (let i = 0; i < MYurldataGRAS5.length; i++) {
          //设备数组
          let mydevlistGRAS5 = MYurldataGRAS5[i].dev
          //设备默认
          let GRAS5devvalue = mydevlistGRAS5.filter((devitem) => (devitem.default == 1));
          //通道默认
          let GRAS5chvalue = mydevlistGRAS5.filter((GRAS5chvalue) => (GRAS5chvalue.name == GRAS5devvalue[0].name))[0].CH.filter((grs5chitem) => (grs5chitem.default == 1))
          // 默认选中项去掉GRAS5
          this.form.MiyunvalueGRAS5 = 'my/' + GRAS5devvalue[0].name + '/' + GRAS5chvalue[0].name
          for (let p = 0; p < mydevlistGRAS5.length; p++) {
            //通道数组
            let mychlistGRAS5 = mydevlistGRAS5[p].CH
            for (let k = 0; k < mychlistGRAS5.length; k++) {
              let myurlobjGRAS5 = {
                value: 'my/' + mydevlistGRAS5[p].name + '/' + mychlistGRAS5[k].name,
                label: mydevlistGRAS5[p].name + '/' + mychlistGRAS5[k].name,
              }
              myurlobjarrGRAS5.push(myurlobjGRAS5)
            }
          }
        }
        this.form.MiyunOptionsGRAS5 = myurlobjarrGRAS5

        //武清 GRAS4
        let wqurlobjarrGRAS4 = []
        let WQurldataGRAS4 = res.data.data.wq.filter((gras) => (gras.gras == 'GRAS4'))
        for (let i = 0; i < WQurldataGRAS4.length; i++) {
          //设备数组
          let wqdevlistGRAS4 = WQurldataGRAS4[i].dev
          //设备默认
          let GRAS4devvalue = wqdevlistGRAS4.filter((devitem) => (devitem.default == 1));
          //通道默认
          let GRAS4chvalue = wqdevlistGRAS4.filter((GRAS4chvalue) => (GRAS4chvalue.name == GRAS4devvalue[0].name))[0].CH.filter((grs4chitem) => (grs4chitem.default == 1))
          // 默认选中项去掉GRAS4
          this.form.wuqingvalueGRAS4 = 'wq/' + GRAS4devvalue[0].name + '/' + GRAS4chvalue[0].name
          for (let p = 0; p < wqdevlistGRAS4.length; p++) {
            //通道数组
            let wqchlistGRAS4 = wqdevlistGRAS4[p].CH
            for (let k = 0; k < wqchlistGRAS4.length; k++) {
              let wqurlobjGRAS4 = {
                value: 'wq/' + wqdevlistGRAS4[p].name + '/' + wqchlistGRAS4[k].name,
                label: wqdevlistGRAS4[p].name + '/' + wqchlistGRAS4[k].name,
              }
              wqurlobjarrGRAS4.push(wqurlobjGRAS4)
            }
          }
        }
        this.form.wuqingOptionsGRAS4 = wqurlobjarrGRAS4
      }).catch(err => {
        console.log(err);
        this.$message.error('获取配置信息失败')
      })
    },
    //配置修改提交
    onSubmit() {
      this.submitLoading = true
      let updatevalue = []
      updatevalue.push({
        path: this.form.MiyunvalueGRAS3,
        gras: 'GRAS3'
      })
      updatevalue.push({
        path: this.form.MiyunvalueGRAS5,
        gras: 'GRAS5'
      })
      updatevalue.push({
        path: this.form.wuqingvalueGRAS4,
        gras: 'GRAS4'
      })

      // 参数
      let newarr = []
      for (let i = 0; i < updatevalue.length; i++) {
        let pathParts = updatevalue[i].path.split('/')
        let updatestr = {
          station: pathParts[0],
          dev: pathParts[1],
          ch: pathParts[2],
          gras: updatevalue[i].gras
        }
        newarr.push(updatestr)
      }

      this.$axios({
        method: "post",
        url: DMSip + '/-/bin.preData.update_conf',
        data: JSON.stringify(newarr),
      }).then(res => {
        this.$message({
          type: 'success',
          message: '配置修改成功'
        });
        this.handleClose()
      }).catch(err => {
        console.log(err);
        this.$message.error('配置修改失败')
      }).finally(() => {
        this.submitLoading = false
      })
    },
    // 关闭弹窗
    handleClose() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
